package com.orientation.h5controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.orientation.commom.Result;
import com.orientation.entity.Goods;
import com.orientation.entity.Reserve;
import com.orientation.service.BedService;
import com.orientation.service.GoodsService;
import com.orientation.service.ReceiveService;
import com.orientation.service.ReserveService;
import com.orientation.vo.BedRoomStudentVo;
import com.orientation.vo.ReceiveGoodsStudentVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @PROJECT_NAME: orientation
 * @PACKAGE_NAME: com.orientation.h5controller
 * @NAME: AppGoodsReceiveController
 * @author:xuandian
 * @DATE: 2022/1/15 15:15
 * @description: 小程序物资申领控制层 报道
 */
@Api(tags = "小程序学生物资接口")
@Validated //数据异常则会统一抛出异常。
@RestController
@RequestMapping("/api")
public class AppGoodsReceiveController {
    @Resource
    private GoodsService goodsService;
    @Resource//物质领取状态表
    private ReceiveService receiveService;

    @Resource //报到
    private ReserveService reserveService;

    @Resource //床位
    private BedService bedService;

    @ApiOperation("根据学号进行物资可选列表")
    @GetMapping("/queryGoods")
    public ResponseEntity<Result> queryGoods(@RequestParam(value = "studentId") String studentId) {
        List<Goods> goodsList = goodsService.queryGoods(studentId);
        return ResponseEntity.ok(Result.success(goodsList));
    }

    @ApiOperation("查询单条")
    @GetMapping("/findById")
    public ResponseEntity<Result> findById(@RequestParam(value = "sid") String sid) {
        return ResponseEntity.ok(Result.success(goodsService.getById(sid)));
    }

    @ApiOperation("物资申领查询单条")
    @GetMapping("/receive/receiveById")
    public ResponseEntity<Result> findReserveById(@RequestParam(value = "sid") String sid) {
        return ResponseEntity.ok(Result.success(receiveService.getById(sid)));
    }

    @ApiOperation("添加物资申领")
    @GetMapping("/insertReceiveOne")
    public ResponseEntity<Result> insertReceiveOne(@RequestParam(value = "goodsId") String goodsId,
                                                   @RequestParam(value = "studentId") String studentId) {
        receiveService.insertReceiveOne(goodsId, studentId);
        return ResponseEntity.ok(new Result());
    }

    @ApiOperation("物资申领列表")
    @GetMapping("/receive/queryReceiveGoodsStudentVo")
    public ResponseEntity<Result> queryReceiveGoodsStudentVo(
            @RequestParam(required = false, value = "receiveStatus") Integer receiveStatus,
            @RequestParam(value = "studentId", required = true) String studentId) {
        IPage<ReceiveGoodsStudentVo> receiveGoodsStudentVoIPage = receiveService.queryReceiveGoodsStudentVo(1, 100, receiveStatus, null, null, null,null, studentId);
        return ResponseEntity.ok(Result.success(receiveGoodsStudentVoIPage.getRecords().stream().sorted(Comparator.comparingInt(ReceiveGoodsStudentVo::getReceiveStatus)).collect(Collectors.toList())));
    }

    @ApiOperation("物资领取登记")
    @PostMapping("/receive/updateOrinsertReceive")
    public ResponseEntity<Result> updateOrinsertReceive(@RequestParam(value = "files", required = false) MultipartFile[] files,
                                                        @RequestParam(value = "sid",required = false) String sid,
                                                        @RequestParam(value = "goodsId") String goodsId,
                                                        @RequestParam(value = "studentId") String studentId) {
        receiveService.updateOrinsertReceiveH(files, sid, goodsId, studentId);
        return ResponseEntity.ok(new Result());
    }

    @ApiOperation("添加报道预约登记")
    @PostMapping("/reserve/insertSave")
    public ResponseEntity<Result> SaveOrUpdate(@RequestBody Reserve reserve) {
        reserveService.insertSave(reserve);
        return ResponseEntity.ok(new Result());
    }

    @ApiOperation("报道审核擦查询")
    @GetMapping("/reserve/queryReserve")
    public ResponseEntity<Result> queryReserve(@RequestParam(value = "studentId") String studentId) {
        Reserve reserve = reserveService.getOne(Wrappers.<Reserve>lambdaQuery().eq(Reserve::getStudentId, studentId));
        return ResponseEntity.ok(Result.success(reserve));
    }

    @ApiOperation("房价床位信息查询")
    @GetMapping("/room/queryByStudentId")
    public ResponseEntity<Result> queryByStudentId(@RequestParam(value = "studentId") String studentId) {
        BedRoomStudentVo bedRoomStudentVo = bedService.queryByStudentId(studentId);
        return ResponseEntity.ok(Result.success(bedRoomStudentVo));
    }

    @ApiOperation("根据学号进行物资可选列表")
    @GetMapping("/queryGoods/getAllGoods")
    public ResponseEntity<Result> getAllGoods() {
        List<Goods> goodsList = goodsService.list();
        return ResponseEntity.ok(Result.success(goodsList));
    }
}
